package com.attendance.sz.utils;

import java.util.Timer;
import java.util.TimerTask;

import com.attendance.sz.R;

import android.app.Activity;
import android.app.Dialog;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.text.method.PasswordTransformationMethod;
import android.text.method.TransformationMethod;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;
public class MyModelDialog extends Dialog
{
	DialogResult dialogResult;
	Handler mHandler ;
	Handler mHandlerMy ;
	Timer timer;
	TimerTask timerTask;
	int isTime = 0;
	TextView txtTime;
	
	
	
	public MyModelDialog(Activity context, int theme, boolean retry)
	{
		
		super(context, theme);
		setOwnerActivity(context);
		onCreate();
		
	}
	
	public DialogResult getDialogResult()
	{
		return dialogResult;
	}
	public void setDialogResult(DialogResult cancel)
	{
		this.dialogResult = cancel;
	}
	/** Called when the activity is first created. */
	
	public void onCreate() {
		setContentView(R.layout.dialog_thecountdown);
		txtTime = (TextView)findViewById(R.id.txtTime);
		isTime = 10; 
		
		mHandlerMy = new Handler() {
			// 定义处理信息的方法
			public void handleMessage(Message msg) {

				switch (msg.what) {
				case 1:
					if ( isTime <= 0 ){
						endDialog(DialogResult.OK);
					}
					txtTime.setText("倒计时 "+isTime+" 秒重启");
					isTime -= 1;
					break;
				}
			}
		};
		timerTask = new TimerTask() {
			@Override
			public void run() {
				mHandlerMy.sendEmptyMessage(1);
			}
		};
		timer = new Timer();
		timer.schedule(timerTask, 1, 1 * 1000);
		
		if (MyApplication.getApp().getSimulater()){
			findViewById(R.id.btnCancel).setEnabled(true);
		}
		else{
			findViewById(R.id.btnCancel).setEnabled(false);
		}
		findViewById(R.id.btnCancel).setOnClickListener(new android.view.View.OnClickListener() {
			
			@Override
			public void onClick(View paramView)
			{
				endDialog(DialogResult.CANCEL);
			}
			});
		
		if (MyApplication.getApp().getSimulater()){
			findViewById(R.id.btnOk).setEnabled(true);
		}
		else{
			findViewById(R.id.btnOk).setEnabled(false);
		}
		findViewById(R.id.btnOk).setOnClickListener(new android.view.View.OnClickListener() {
			
			@Override
			public void onClick(View paramView)
			{
				endDialog(DialogResult.OK);
			}
			});
		}
	
	public void endDialog(DialogResult cancel)
	{
		if (timer != null) {
			timer.cancel();
			timer = null;
		}

		if (timerTask != null) {
			timerTask.cancel();
			timerTask = null;
		}
		dismiss();
		setDialogResult(cancel);
		Message m = mHandler.obtainMessage();
		mHandler.sendMessage(m);
	}
	
	public DialogResult showDialog()
	{
		
		mHandler = new Handler() {
			@Override
              public void handleMessage(Message mesg) {
                  // process incoming messages here
				//super.handleMessage(msg);
				if ( mesg.what == 1 ){
					if ( isTime <= 0 ){
						endDialog(DialogResult.OK);
					}
					txtTime.setText("倒计时 "+isTime+" 秒");
					isTime -= 1;
				}
				throw new RuntimeException();
              }
          };
		super.show();
		try {
			Looper.getMainLooper().loop();
		}
		catch(RuntimeException e2)
		{
		}
		return dialogResult;
		
		
	}
	
}
